<template>
    <el-dialog @open="open" v-model="isOpen" title="分配采购单" @close="close" style="width: 450px;">
        <el-select v-model="userId" placeholder="选择采购员" style="width: 240px">
            <el-option
                    v-for="user in users"
                    :key="user.id"
                    :label="user.username"
                    :value="user.id"
            >
                <span style="float: left">{{ user.username }}</span>
                <span style="float: right;color: var(--el-text-color-secondary);font-size: 13px;">
                    {{ user.nickName }}
                  </span>
            </el-option>
        </el-select>

        <el-button type="primary" style="margin-left: 15px" activity @click="doDistribute">确定</el-button>
        <el-button @click="close" type="success" activity >取消</el-button>
    </el-dialog>
</template>

<script setup>
    import {ref,reactive} from 'vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    const  emits = defineEmits(['close','reload'])

    let dataForm = ref(null)
    let props = defineProps(['openType','id'])
    let userId = ref()
    let users = ref([])
    let isOpen = ref(false)
    defineExpose({isOpen})

    function open(){
        request.get(`/api/common/getUsersByRoleCode/purchaser`).then(resp => {
            userId.value=null
            users.value.length=0
            users.value.push(...resp)
        })
    }

    function close(){
        isOpen.value = false
        emits("close")
    }

    function doDistribute(){
        let param = {userId:userId.value, purchaseId: props.id}
        if (userId.value) {
            request.post("/api/wmsPurchase/distribute", param).then(resp => {
                emits("close")
                emits("reload")
                ElMessage({message: '分配成功！',type: 'success'})
            })
        }else{
            ElMessage({message: '请选择！',type: 'error'})
        }

    }
</script>

<style scoped>

</style>